<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Ajax.php
 *
 * @author Simon Emms <simon@simonemms.com>
 */
class Ajax {

    /**
     * Where the CI object will be stored
     *
     * @var object
     */
    public $_objCI;


    /**
     * Construct
     *
     * Get the instance of CI
     */
    public function __construct() {
        $this->_objCI = &get_instance();
    }



    /**
     * AJAX Exists
     *
     * Checks that the AJAX method/controller exists. If
     * not, it returns you to the homepage.
     *
     * @param string $method
     * @author Simon Emms <simon@simonemms.com>
     */
    public function ajax_exists($method, $admin = false) {
        if($admin !== false) {
            /* Ensure that we're logged into the admin side */
            $this->_objCI->load->model('users');
            if($this->_objCI->users->isAdminLoggedIn() === false) {
                /* Not logged in */
                redirect(ajax_url());
            }
        }
        
        if(method_exists($this->_objCI->router->class, $method) === false) {
            redirect(ajax_url());
        }
    }



    /**
     * Check
     *
     * Shorthand check to allow you to see if a user
     * has the right priviliges to view a page.  Enter
     * in the required parameters and specify POST
     * or GET.  If it fails, it returns you to the homepage.
     *
     * Returns all the required fields, nicely cleaned.
     *
     * @param array $arrRequired
     * @param string $type
     * @param bool $not_empty
     * @return array
     * @access private
     * @author Simon Emms <simon@simonemms.com>
     */
    public function check(array $arrRequired, $type = 'post', $not_empty = true) {
        $arrTypes = array('post', 'get');
        $type = strtolower($type);
        /* Check that we're using a valid type */
        if(in_array($type, $arrTypes)) {
            if(count($arrRequired) > 0) {
                $input = true;
                $arrDetails = array();
                foreach($arrRequired as $required) {
                    if($input) {
                        /* Checks that the input field is set and not empty */
                        $input = $this->_objCI->input->$type($required, true);
                        if($input !== false) {
                            if($not_empty) {
                                if(!empty($input) || $input == '0') {
                                    $input = true;
                                    $arrDetails[$required] = $this->_objCI->input->$type($required, true);
                                } else {
                                    $input = false;
                                }
                            } else {
                                $input = true;
                                $arrDetails[$required] = $this->_objCI->input->$type($required, true);
                            }
                        }
                    }
                }
                /* If $input is still true, we've won */
                if($input) { return $arrDetails; }
            }
        }

        /* If we get here, the validation has failed */
        redirect(ajax_url());
    }

}
?>